package com.xnx3.banmahuishou.superadmin.controller;

import java.util.List;
import java.util.Random;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.BaseVO;
import com.xnx3.DateUtil;
import com.xnx3.Lang;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.IpUtil;
import com.xnx3.j2ee.util.LanguageUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 运维管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouSuperadminMaintainController")
@RequestMapping("/superadmin/maintain/")
public class MaintainController extends BaseController {
	@Resource
	private SqlService sqlService;	
	
	/**
	 * 运维列表
	 */
	@RequestMapping("list${url.suffix}")
	public String listv2(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"nickname"});
		sql.appendWhere("authority = 13");
		int count = sqlService.count("user", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM user", page);
		sql.setDefaultOrderBy("user.id DESC");
		List<User> list = sqlService.findBySql(sql, User.class);
		
		ActionLogUtil.insert(request, "运维管理", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/superadmin/maintain/list";
	}

	/**
	 * 新增/修改运维信息
	 * @param id 要修改的id 。 如果是新增，这里传入0或者不传
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(
			@RequestParam(value = "id", required = false, defaultValue="0") int id,
			Model model, HttpServletRequest request){
		User entity;
		
		if(id < 1){
			//新增
			entity = new User();
			ActionLogUtil.insert(request, "进入运维新增页面");
		}else{
			//修改
			entity = sqlService.findById(User.class, id);
			if(entity == null){
				return error(model, "要修改的运维不存在");
			}
			ActionLogUtil.insert(request, "进入修改运维页面", entity.toString());
		}
		
		//编辑页面
		model.addAttribute("entity", entity);
		return "/superadmin/maintain/edit";
	}
	
	/**
	 * 新增、修改的运维信息进行保存
	 */
	@RequestMapping(value="save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO variableSavev2(User inputEntity, HttpServletRequest request){
		User entity = null;			
		
		if(inputEntity.getId() == null || inputEntity.getId() < 1){
			//新增
			entity = new User();
			// 新增之前先判断传入的手机号是否已有 后台 用户在使用
			if(inputEntity.getPhone() != null && inputEntity.getPhone().length() > 0){
				if(findByPhone(inputEntity.getPhone()) != null){
					return BaseVO.failure(LanguageUtil.show("user_regFailureForPhoneAlreadyExist"));
				}
			}
			// 判断手机号是否合法
			if(!isMobiPhoneNum(inputEntity.getPhone())) {
				return error("手机号不合法");
			}
			Random random = new Random();
			entity.setUsername(inputEntity.getPhone());
			entity.setPassword(Lang.uuid());
			entity.setRegtime(DateUtil.timeForUnix10());	
			entity.setHead("http://cdn.banxma.com/file/images/a63f71f4eea14157975b6fee1a188cb1.png");
			entity.setAuthority("13");
			entity.setAllSellWgight(0);
			entity.setAllOrderNumbe(0);
			entity.setAllIncomeMone(0);
			entity.setMoney(0);
			entity.setFreezemoney(0);
			entity.setLasttime(DateUtil.timeForUnix10());
			entity.setRegip(IpUtil.getIpAddress(request));
			entity.setLastip(IpUtil.getIpAddress(request));
			entity.setCurrency(0);
			entity.setReferrerid(getUserId());
			entity.setIsfreeze(User.ISFREEZE_NORMAL);
			entity.setSalt(random.nextInt(10)+""+random.nextInt(10)+""+random.nextInt(10)+""+random.nextInt(10)+"");
			
		}else{
			//修改
			entity = sqlService.findById(User.class, inputEntity.getId());
			if(entity == null){
				return error("要修改的目标不存在");
			}
			// 修改之前先判断传入的手机号是否已有 后台 用户在使用
			if(inputEntity.getPhone() != null && inputEntity.getPhone().length() > 0){
				if(findByPhone(inputEntity.getPhone(),entity.getId()) != null){
					return BaseVO.failure(LanguageUtil.show("user_regFailureForPhoneAlreadyExist"));
				}
			}
			// 判断手机号是否合法
			if(!isMobiPhoneNum(inputEntity.getPhone())) {
				return error("手机号不合法");
			}

		}
		if(!StringUtils.hasLength(inputEntity.getNickname())) {
			return error("请输入名称");
		}
		if(!StringUtils.hasLength(inputEntity.getPhone())) {
			return error("请输入手机号");
		}
		if(!StringUtils.hasLength(inputEntity.getEmail())) {
			return error("请输入邮箱");
		}
		//保存
		entity.setNickname(inputEntity.getNickname());
		entity.setPhone(inputEntity.getPhone());
		entity.setEmail(inputEntity.getEmail());
		
		sqlService.save(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存运维信息", entity.toString());
		return success();
	}
	
	/**
	 * 删除运维信息
	 */
	@RequestMapping(value="delete.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO delete(@RequestParam(value = "id", required = false, defaultValue="0") int id, HttpServletRequest request){
		User entity = sqlService.findById(User.class, id);
		if(entity == null){
			return error("要删除的目标不存在");
		}
		sqlService.delete(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "删除运维", entity.toString());
		return success();
	}
}
